using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Stocks;
namespace StockAnalyze
{
    public partial class PriceGraph : UserControl
    {
        private StockLog _log = null;
        private DateTime _startDate = DateTime.MinValue;
        private DateTime _endDate = DateTime.MinValue ;

        public DateTime StartDate
        {
            get { return _startDate; }
            set { _startDate = value;
            this.Invalidate();
        }
        }
        public DateTime EndDate
        {
            get { return _endDate; }
            set { _endDate = value;
            this.Invalidate();
        }

        }
        public StockLog StockData
        {
            get { return _log; }
            set { _log = value;
            this.Invalidate();
        }
        }

        public PriceGraph()
        {
            InitializeComponent();
        }

        private void PriceGraph_Paint(object sender, PaintEventArgs e)
        {

            Rectangle rectXYLines = this.XYLinesRectangle();
            if (e.ClipRectangle.IntersectsWith(rectXYLines))
                drawXYLines(e.Graphics, rectXYLines);
            
            drawXScale(e.Graphics, rectXYLines);
            
        }
        private Rectangle XYLinesRectangle()
        {
            Rectangle r = new Rectangle(new Point(this.Left + 5, 0), new Size(this.Width - 60, this.Height -20));
            return r;
        }
        private void drawXYLines(Graphics g, Rectangle r)
        {
            Point[] points = new Point[4];
            points[0] = r.Location;
            points[1] = new Point(r.Left, r.Bottom);
            points[2] = new Point(r.Right, r.Bottom);
            points[3] = new Point(r.Right, r.Top);
            g.DrawLines(Pens.Red, points);
            
        }
        private void drawXScale(Graphics g, Rectangle grid)
        {
            float tickWidth;
            float tickOffset;
            float tickPos;
            int tickY0 = grid.Bottom -2;
            int tickY1 = grid.Bottom +2;
            TimeSpan ts = this.EndDate.Subtract(this.StartDate);
            int numDays = ts.Days;
            if (this.StartDate.Date < this.EndDate.Date)
            {
                tickWidth = ((float)grid.Width / (float)numDays);
                tickOffset = (float)(tickWidth / 2);
                for (int i = 0; i < numDays; i++)
                {
                    // draw ticks
                    tickPos = i * tickWidth + tickOffset + grid.Left;
                    g.DrawLine(Pens.Red, tickPos, tickY0, tickPos, tickY1);
                }
            }
            if (this._log != null)
            {
                // Draw log data
            }
        }

    }
}
